﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    public class SQLHelper
    {
        private static string constr = "server = LAPTOP-TSVKIEVL ; uid = sa ; pwd = 123456 ; database = Student";
        private SqlConnection con = null;

        public SQLHelper() 
        {
            con = new SqlConnection(constr);
        }
        public DataSet Get(string sql , SqlParameter[] pars) 
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand(sql, con);
                if (pars != null)
                {
                    cmd.Parameters.AddRange(pars);
                }
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();

                adapter.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            finally 
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
        public int Execute(string sql, SqlParameter[] pars) 
        {
            int result = -1;
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlCommand cmd = new SqlCommand(sql, con);
                if (pars != null)
                {
                    cmd.Parameters.AddRange(pars);
                }
                result = cmd.ExecuteNonQuery();

                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally 
            {
                if (con != null)
                {
                    con.Close();
                }
            }
        }
    }
}